$(function () {
  //去购物车
  $(".cart").click(function () {
    window.location.href = "shopingcar.html";
  });
  //点击去登录
  $(".login").bind("click", function () {
    window.location.href = "login.html";
  });
  //去注册
  $(".regedit").click(function () {
    window.location.href = "regedit.html";
  });
  let a = $(".remen a:not(.frast)");
  for (let i = 0; i < a.length; i++) {
    a[i].style.color = "red";
    a[i].style.margin = "20px 5px 0 0";
  }
  //注册账号

  $(".form-control").change(function () {
    $(".tishi:first").text("");
    if (/[\d]/g.test($(".form-control").val())) {
      $(".tishi:first").text("账号格式不正确");
    }
  });
  $(".form-control2").change(function () {
    $(".tishi:eq(1)").text("");
    if (!/^\w{6,}$/g.test($(".form-control2").val())) {
      $(".tishi:eq(1)").text("密码至少六位");
    }
  });

  $("#bbt").click(function () {
    if ($(".form-control").val() == "" || $(".form-control2").val() == "") {
      alert("用户名和密码不能为空");
    } else {
      $.ajax({
        type: "get",
        url: "http://jx.xuzhixiang.top/ap/api/reg.php",
        data: {
          username: $(".form-control").val(),
          password: $(".form-control2").val(),
        },
        dataType: "json",
        success: function (response) {
          console.log(response);
          alert(response.msg);
          window.location.href = "login.html";
        },
      });
      console.log(444);
    }
  });
  //登录账号

  $("#btn").click(function () {
    if ($("#aa").val() == "" || $("#bb").val() == "") {
      alert("用户名和密码不能为空");
    } else {
      $.ajax({
        type: "get",
        url: "http://jx.xuzhixiang.top/ap/api/login.php",
        data: {
          username: $("#aa").val(),
          password: $("#bb").val(),
        },
        dataType: "json",
        success: function (response) {
          console.log("44");
          console.log(response.data);
          if (response.code == 1) {
            localStorage.setItem("userId", response.data.id);
            localStorage.setItem("name", response.data.username);
            localStorage.setItem("token", response.data.token);
            alert(response.msg);
            window.location.href = "shouye.html";
          } else {
            alert(response.msg);
          }
        },
      });
    }
  });
  if (localStorage.getItem("userId")) {
    $(".login")
      .html("你好！" + localStorage.getItem("name"))
      .click(function () {
        window.location = "shopingcar.html";
      });
    $(".regedit").html("<i>退出登录<i>");
    $(".regedit i")
      .css({ color: "red" })
      .click(function () {
        localStorage.removeItem("userId");
      });
  } else {
    $(".login").html("[登录]");
  }
  //居家列表
  $(".main-nav .n").mouseenter(function () {
    $(".main-center-left").eq(0).addClass("show").removeClass("line");
    $(".main-center-left").eq("1").addClass("line").removeClass("show");
  });
  $(".main-nav .n2").mouseenter(function () {
    $(".main-center-left").eq(0).addClass("line").removeClass("show");
    $(".main-center-left").eq("1").addClass("show").removeClass("line");
  });
  // $(".main-center-left li").mouseenter(function () {
  //   let index = $(this).index();
  //   console.log($(".main-center-left p").eq(index));
  //   $(".main-center-left p")
  //     .eq(index)
  //     .addClass("show")
  //     .siblings()
  //     .removeClass("show");
  // });
  // $(".main-center-left li").mouseover(function () {
  //   $(".main-center-left li p")
  //   .removeClass("show");
  // });
  // $("#floorNav li:last").click(function () {
  //   $(window).scrollTop(0);
  // });
});
